﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace TB_WS.Class
{
    public class Price_Push
    {
        public static int insert_PP_Link(string URL,string Goods_ID,
            string Goods_Name,string Seller_Nick,string Link_Type)
        {
            string sql = "insert into Price_Push_Link values('" + URL +
                "','" + Goods_ID + "','" + Goods_Name + "','" + Seller_Nick + "','" + Link_Type + "','','')";
            return Class.Unit.db_Conn.Nonquery(sql);
        }
        public static DataSet get_PP_Link()
        {
            string sql = "select * from Price_Push_Link order by id desc";
            return Class.Unit.db_Conn.select(sql);
        }
        public static DataSet get_PP_Link(string Link_Type)
        {
            string sql = "select * from Price_Push_Link where Link_Type='"+Link_Type+"' order by id desc";
            return Class.Unit.db_Conn.select(sql);
        }

        public static int Get_Link_Count(string URL)
        {
            string sql = "select count(*) from Price_Push_Link where URL='" + URL + "'";
            return Convert.ToInt32(Class.Unit.db_Conn.scalar(sql));
        }

        public static int Update_Link(string Goods_ID,string Price_Str)
        {
            string sql = "update Price_Push_Link set Price_Str='" + Price_Str + "',Last_Update='" + DateTime.Now.ToString() + "' where Goods_ID='" + Goods_ID + "'";
            return Class.Unit.db_Conn.Nonquery(sql);
        }

        public static DataSet Get_Push_URL_Use_Goods(string Goods_ID)
        {
            string sql = "select Robot_URL from PP_Follow_List,Price_Push_Link,DingTalk_Robot,Robot_Type where PP_Follow_List.Link_ID=Price_Push_Link.id and PP_Follow_List.Robot_Type_ID=Robot_Type.id and DingTalk_Robot.Type_ID = Robot_Type.id and Robot_Type.Type_Name = '竞店价格监控' and DingTalk_Robot.Seller_ID=PP_Follow_List.Seller_ID and Price_Push_Link.Goods_ID = '" + Goods_ID + "'";
            return Class.Unit.db_Conn.select(sql);
        }

        public static DataSet Get_PP_Link_Info(string Goods_ID)
        {
            string sql = "select * from Price_Push_Link where Goods_ID='" + Goods_ID + "'";
            return Class.Unit.db_Conn.select(sql);
        }

        public static DataSet Get_SKU_Push(string Goods_ID)
        {
            string sql = "select Robot_URL from PP_Follow_List,Price_Push_Link,DingTalk_Robot,Robot_Type where PP_Follow_List.Link_ID=Price_Push_Link.id and DingTalk_Robot.Type_ID = Robot_Type.id and Robot_Type.Type_Name = 'SKU级价格监测' and DingTalk_Robot.Seller_ID=PP_Follow_List.Seller_ID and Price_Push_Link.Goods_ID = '" + Goods_ID + "'";
            return Class.Unit.db_Conn.select(sql);
        }

        public static int Insert_PP_Follow(int Link_ID,int Seller_ID)
        {
            string sql = "select count(*) from PP_Follow_List where Link_ID=" + Link_ID.ToString() + " and Seller_ID=" + Seller_ID.ToString();
            int PP_FW_Count = Convert.ToInt32(Class.Unit.db_Conn.scalar(sql));
            if(PP_FW_Count>0)
            {
                //已经存在了
                return -1;
            }
            else
            {
                string sql_2 = "insert into PP_Follow_List values(" + Link_ID + "," + Seller_ID + ",1)";
                return Class.Unit.db_Conn.Nonquery(sql_2);
            }
        }
        public static DataSet Get_Follow_Link(string Seller_Nick)
        {
            string sql = "select Price_Push_Link.id,URL,Goods_Name,Price_Push_Link.Seller_Nick,Last_Update from PP_Follow_List,Seller_Info,Price_Push_Link where PP_Follow_List.Seller_ID=Seller_Info.id and PP_Follow_List.Link_ID=Price_Push_Link.id and Seller_Info.Seller_Nick='" + Seller_Nick + "'";
            return Class.Unit.db_Conn.select(sql);
        }

        public static int UN_Follow_Link(int Link_ID,int Seller_ID)
        {
            string sql = "delete from PP_Follow_List where Link_ID=" + Link_ID + " and Seller_ID=" + Seller_ID;
            return Class.Unit.db_Conn.Nonquery(sql);
        }

        public static DataSet Get_SKU_Info(string SKU_Key)
        {
            string sql = "select * from PP_SKU_Map where SKU_Key='" + SKU_Key + "'";
            return Class.Unit.db_Conn.select(sql);
        }
        public static string Process_SKU_Map(string SKU_Map)
        {
            string RT_SKU_Str = "";
            string[] SKU_List = SKU_Map.Split('@');
            for(int i=0;i<SKU_List.Length-1;i++)
            {
                if(Get_SKU_Info(SKU_List[i].Split('&')[0].ToString()).Tables[0].Rows.Count==0)
                {
                    //不存在这个SKU信息
                    string sql = "insert into PP_SKU_Map values('" + SKU_List[i].Split('&')[0].ToString()
                        + "','" + SKU_List[i].Split('&')[1].ToString() + "','" +
                        DateTime.Now.ToString() + "')";
                    RT_SKU_Str = RT_SKU_Str + "\n\n新增SKU属性：" + SKU_List[i].Split('&')[1].ToString();
                    Class.Unit.db_Conn.Nonquery(sql);
                }
                else
                {
                    DataSet SKU_Info_DST = Get_SKU_Info(SKU_List[i].Split('&')[0].ToString());
                    if (SKU_Info_DST.Tables[0].Rows[0][2].ToString()== SKU_List[i].Split('&')[1].ToString())
                    {
                        //SKU信息相同
                    }
                    else
                    {
                        //SKU信息不同
                        string sql = "update PP_SKU_Map set SKU_Value='" + SKU_List[i].Split('&')[1].ToString() + "',Last_Update='" + DateTime.Now.ToString() + "' where SKU_Key='" + SKU_List[i].Split('&')[0].ToString() + "'";
                        Class.Unit.db_Conn.Nonquery(sql);
                        RT_SKU_Str = RT_SKU_Str + "\n\nSKU属性发生变更 原SKU信息：" + SKU_Info_DST.Tables[0].Rows[0][2].ToString() + "\t现SKU信息：" +
                            SKU_List[i].Split('&')[1].ToString();
                    }
                }
            }
            return RT_SKU_Str;
        }

        public static DataSet Get_Price_Info(string Price_Key,string Goods_ID)
        {
            string sql = "select * from PP_Price_Map where Price_Key='" + Price_Key + "' and Goods_ID='"+Goods_ID+"'";
            return Class.Unit.db_Conn.select(sql);
        }
        public static string Process_Price_Map(string Goods_ID,string Price_Map)
        {
            //Class.Unit.INSERT_LOG("进入Process.Price");
            string RT_PP_String = "";
            string RT_DK_STR = "";
            try
            {
                string[] Price_List = Price_Map.Split('$');
                Class.Unit.INSERT_LOG("共获取到价格列表List：" + Price_List.Length.ToString() + "个");
                for (int i = 0; i < Price_List.Length; i++)
                {
                    string Price_Key = Price_List[i].Split('&')[0].ToString();

                    string New_Price = Price_List[i].Split('&')[1].ToString();

                    DataSet Price_DST = Get_Price_Info(Price_Key,Goods_ID);
                    if (Price_DST.Tables[0].Rows.Count == 0)
                    {

                        //没有这个数据
                        string sql = "insert into PP_Price_Map values('" + Goods_ID + "','" + Price_Key + "','" + New_Price + "','" + DateTime.Now.ToString() + "')";
                        Class.Unit.db_Conn.Nonquery(sql);
                        string SKU_Name = "";

                        string[] Price_Key_List = Price_Key.Split('@');
                        for (int j = 0; j < Price_Key_List.Length; j++)
                        {

                            SKU_Name = SKU_Name + "--" + Get_SKU_Info(Price_Key_List[j].ToString()).Tables[0].Rows[0][2].ToString();
                        }

                        RT_PP_String = RT_PP_String + "\n\n【新增】新增SKU价格 SKU名称：" + SKU_Name + "价格：" + New_Price;

                    }
                    else
                    {

                        if (Price_DST.Tables[0].Rows[0][3].ToString() == New_Price)
                        {
                            //价格相同
                        }
                        else
                        {
                            //价格不同
                            string sql = "update PP_Price_Map set Price_Value='" + New_Price + "',Last_Update='" + DateTime.Now.ToString() + "' where Price_Key='" + Price_Key + "' and Goods_ID="+Goods_ID;
                            Class.Unit.db_Conn.Nonquery(sql);
                            string SKU_Name = "";

                            string[] Price_Key_List = Price_Key.Split('@');
                            for (int j = 0; j < Price_Key_List.Length; j++)
                            {
                                SKU_Name = SKU_Name + "--" + Get_SKU_Info(Price_Key_List[j].ToString()).Tables[0].Rows[0][2].ToString();
                            }

                            RT_PP_String = RT_PP_String + "\n\n【更新】" + SKU_Name + " 原价格：" + Price_DST.Tables[0].Rows[0][3].ToString() +
                                "新价格：" + New_Price;
                        }
                    }


                }
                DataSet Robot_Push_URL = Class.Price_Push.Get_SKU_Push(Goods_ID);
            DataSet Goods_DST = Get_PP_Link_Info(Goods_ID);
                for (int k= 0; k < Robot_Push_URL.Tables[0].Rows.Count; k++)
            {
                    string PP_Title = Goods_DST.Tables[0].Rows[0][3].ToString();
                    string PP_Nick = Goods_DST.Tables[0].Rows[0][4].ToString();

                    RT_DK_STR = "【SKU级价格推送】\n\n标题：" + PP_Title +
                    "\n卖家昵称：" + PP_Nick + "\n\n" +
                    RT_PP_String + "\n\n当前时间：" + DateTime.Now.ToString();
                    Class.Unit.INSERT_LOG(DateTime.Now.ToString() + "断点：" + RT_DK_STR);
                    Class.Unit.INSERT_LOG("返回值："+
                    LeShiTao_Library.DingTalk.Query_Message.Send_Roboot_Message(Robot_Push_URL.Tables[0].Rows[k][0].ToString(), RT_DK_STR));
            }
            }
            catch (Exception ex)
            {
                Class.Unit.INSERT_LOG(DateTime.Now.ToString() + "断点：进入真断点了"+ex.StackTrace.ToString());
                //LeShiTao_Library.DingTalk.Query_Message.Send_Roboot_Message("https://oapi.dingtalk.com/robot/send?access_token=76f53ab7120b9c91185cf503e86fbc7716b02443393b07f563ff876c97c1d17d",ex.Message.ToString()+ex.StackTrace.ToString());
            }
            
            //Class.Unit.INSERT_LOG(DateTime.Now.ToString()+"断点："+RT_PP_String);
            //Class.Unit.INSERT_LOG(DateTime.Now.ToString() + "断点：H");
            return "Success";
        }
        public static int Match_Price_Str(string Goods_ID,string Price_Str,string SKU_Map)
        {
            string sql = "select Price_Str from Price_Push_Link where Goods_ID='" + Goods_ID + "'";
            string Old_Price_Str = Class.Unit.db_Conn.scalar(sql).ToString();
            if(Old_Price_Str==Price_Str)
            {
                //无变化
                return 0;
            }
            else
            {
                //有变化
                Update_Link(Goods_ID, Price_Str);
                DataSet Robot_Push_URL = Class.Price_Push.Get_Push_URL_Use_Goods(Goods_ID);
                DataSet Goods_DST = Get_PP_Link_Info(Goods_ID);
                for(int i=0;i<Robot_Push_URL.Tables[0].Rows.Count;i++)
                {
                    LeShiTao_Library.DingTalk.Query_Message.Send_Roboot_Message(Robot_Push_URL.Tables[0].Rows[i][0].ToString(),
                        "标题：" + Goods_DST.Tables[0].Rows[0][3].ToString() +
                        "\n卖家昵称：" + Goods_DST.Tables[0].Rows[0][4].ToString() + "\n价格有变化啦"+
                        Process_SKU_Map(SKU_Map)+"\n\n当前时间："+DateTime.Now.ToString());
                }
                Process_Price_Map(Goods_ID, Price_Str);
                return 1;
            }
        }

        public static string Push_Type(string Type_Name,string Push_Str)
        {
            string sql = "select Robot_URL from DingTalk_Robot,Robot_Type where Type_ID=Robot_Type.id and Robot_Type.Type_Name='" + Type_Name + "'";
            DataSet URL_DST = Class.Unit.db_Conn.select(sql);
            for(int i=0;i<URL_DST.Tables[0].Rows.Count;i++)
            {
                LeShiTao_Library.DingTalk.Query_Message.Send_Roboot_Message(
                    URL_DST.Tables[0].Rows[i][0].ToString(), Push_Str);
            }
            return "Success";
        }

        public static DataSet Get_PP_Link_Type()
        {
            string sql = "select * from PP_Link_Type";
            return Class.Unit.db_Conn.select(sql);
        }
    }
}
